package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.QuickDebitFeeRateData;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.List;
import java.util.Locale;

/**
 * Created by Jiangzx on 2017/9/21 19:17.
 */

public class QuickDebitFeeRateAdapter extends RecyclerView.Adapter {
    public static class FeeRateBean{
        public static final int TYPE_ITEM=0;
        public static final int TYPE_FOOTER=1;
        int type;
        QuickDebitFeeRateData.FeeRate bean;
        public FeeRateBean(int type,QuickDebitFeeRateData.FeeRate bean){
            this.type=type;
            this.bean=bean;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public QuickDebitFeeRateData.FeeRate getBean() {
            return bean;
        }

        public void setBean(QuickDebitFeeRateData.FeeRate bean) {
            this.bean = bean;
        }
    }

    private class ContentHolder extends RecyclerView.ViewHolder{
        ImageView ivLogo;
        TextView tvTitle;
        TextView tvArrival;
        TextView tvRate;
        TextView tvSettlement;
        TextView tvQuota;
        public ContentHolder(View itemView) {
            super(itemView);
            ivLogo= (ImageView) itemView.findViewById(R.id.iv_logo);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvArrival= (TextView) itemView.findViewById(R.id.tv_date_arrival);
            tvRate= (TextView) itemView.findViewById(R.id.tv_rate);
            tvSettlement= (TextView) itemView.findViewById(R.id.tv_settlement);
            tvQuota= (TextView) itemView.findViewById(R.id.tv_quota);
        }
    }

    private class FooterHolder extends RecyclerView.ViewHolder{
        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

    private List<FeeRateBean> data;
    public QuickDebitFeeRateAdapter(List<FeeRateBean> data){
        this.data=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==FeeRateBean.TYPE_ITEM) {
            return new ContentHolder(inflater.inflate(R.layout.item_fee_rate_content,parent,false));
        }
        if(viewType==FeeRateBean.TYPE_FOOTER){
            return new FooterHolder(inflater.inflate(R.layout.item_fee_rate_footer,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ContentHolder){
            if(position==0){
                RecyclerView.MarginLayoutParams params=
                        (RecyclerView.MarginLayoutParams) holder.itemView.getLayoutParams();
                params.topMargin= DensityUtil.dp2px(15);
            }
            ContentHolder cHolder= (ContentHolder) holder;
            QuickDebitFeeRateData.FeeRate rate=data.get(position).getBean();
            ImageLoaderUtil.displayImage(
                    URLUtil.CBB_IMG_URL+rate.getPicture_path(),
                    cHolder.ivLogo);
            cHolder.tvTitle.setText(rate.getChannel_name());
            cHolder.tvArrival.setText(
                    "到账日期："+
                            ("1".equals(rate.getSettlement())?"T0":"T1"));
            cHolder.tvRate.setText(
                    String.format(Locale.CHINESE,Constant.AMOUNT_FORMAT,Double.parseDouble(rate.getRate())) +"%");
            cHolder.tvSettlement.setText(
                    String.format(Locale.CHINESE,Constant.AMOUNT_FORMAT,Double.parseDouble(rate.getEach_charge()))+
                            "/笔");
            //判断是否为整数
            try{
                //转成整数
                long amount=Long.parseLong(rate.getHigh_position());
                if(amount>=10000){
                    if(amount%10000==0){
                        cHolder.tvQuota.setText(
                                "当天"+(amount/10000)+"万元");
                    }else{
                        cHolder.tvQuota.setText(
                                "当天"+ String.format(Locale.CHINESE, Constant.AMOUNT_FORMAT,amount/10000.0)
                                        +"万元");
                    }
                }else{
                    cHolder.tvQuota.setText(
                            "当天"+amount+"元");
                }
            }catch (Exception e){
                //转成小数
                cHolder.tvQuota.setText(
                        "当天"+
                                String.format(Locale.CHINESE,Constant.AMOUNT_FORMAT,Double.parseDouble(rate.getHigh_position()))+
                                "元");
            }
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }
}
